package cn.sharesdk.onekeyshare.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.KeyEvent;

/**
 * Created by ak
 * Dialog工具类
 */
public class DialogUtil {


    public static ProgressDialog createLoadingProgressDialog(Context context, boolean cancelable) {
        return createLoadingProgressDialog(context, null, null, cancelable);
    }

    public static ProgressDialog createLoadingProgressDialog(Context context, String title, String message, boolean cancelable) {
        int theme = 0;
        if (Build.VERSION.SDK_INT >= 11)
            theme = ProgressDialog.THEME_HOLO_LIGHT;
        return createProgressDialog(context, theme, true, title, 0, true, message, 0, false, cancelable, null, null, null);
    }

    public static ProgressDialog createProgressDialog(
            Context context, int theme,
            boolean needSetTitle, String title, int titleResId,
            boolean needSetMessage, String message, int messageResId,
            boolean indeterminate,
            final boolean cancelable,
            DialogInterface.OnCancelListener cancelListener,
            DialogInterface.OnDismissListener dismissListener,
            final DialogInterface.OnKeyListener keyListener) {
        if (null == context) return null;
        ProgressDialog dialog = null;
        if (theme > 0)
            dialog = new ProgressDialog(context, theme);
        // 构建Title
        if (needSetTitle) {
            if (null != title)
                dialog.setTitle(title);
            else if (titleResId > 0)
                dialog.setTitle(titleResId);
        }
        // 构建Message
        if (needSetMessage) {
            if (null != message)
                dialog.setMessage(message);
            else if (messageResId > 0)
                dialog.setMessage(context.getResources().getText(messageResId));
        }
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setIndeterminate(indeterminate);
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.setOnDismissListener(dismissListener);
        if (!cancelable) {
            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                    // 如果上层进行KeyListener监听,且已消费事件,则true,否则交予Dialog继续执行
                    if (null != keyListener) {
                        boolean consumed = keyListener.onKey(dialogInterface, i, keyEvent);
                        if (consumed) {
                            return true;
                        }
                    }
                    if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
//        FontManager.resetViewFont(dialog.getWindow().getDecorView(),true);
        return dialog;
    }

}
